home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / RuleBasedCollator.java < prev    next >
Text File  |  1998-09-22  |  53KB  |  1,201 lines

  1. /*
  2.  * @(#)RuleBasedCollator.java    1.21 98/02/12
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text;
  32.  
  33. import java.util.Vector;
  34. import java.io.ObjectOutputStream;
  35. import java.io.ObjectInputStream;
  36. import java.io.IOException;
  37.  
  38. /**
  39.  * The <code>RuleBasedCollator</code> class is a concrete subclass of
  40.  * <code>Collator</code> that provides a simple, data-driven, table collator.
  41.  * With this class you can create a customized table-based <code>Collator</code>.
  42.  * <code>RuleBasedCollator</code> maps characters to sort keys.
  43.  *
  44.  * <p>
  45.  * <code>RuleBasedCollator</code> has the following restrictions
  46.  * for efficiency (other subclasses may be used for more complex languages) :
  47.  * <ol>
  48.  * <li>If a French secondary ordering is specified it applies to the
  49.  *     whole collator object.
  50.  * <li>All non-mentioned Unicode characters are at the end of the
  51.  *     collation order.
  52.  * <li>Private use characters are treated as identical.  The private
  53.  *     use area in Unicode is <code>0xE800</code>-<code>0xF8FF</code>.
  54.  * </ol>
  55.  *
  56.  * <p>
  57.  * The collation table is composed of a list of collation rules, where each
  58.  * rule is of three forms:
  59.  * <pre>
  60.  *    < modifier >
  61.  *    < relation > < text-argument >
  62.  *    < reset > < text-argument >
  63.  * </pre>
  64.  * The following demonstrates how to create your own collation rules:
  65.  * <UL Type=round>
  66.  *    <LI><strong>Text-Argument</strong>: A text-argument is any sequence of
  67.  *        characters, excluding special characters (that is, whitespace
  68.  *        characters and the characters used in modifier, relation and reset).
  69.  *        If those characters are desired, you can put them in single quotes
  70.  *        (e.g. ampersand => '&').  Note that unquoted white space characters
  71.  *        are ignored; e.g. <code>b c</code> is treated as <code>bc</code>.
  72.  *    <LI><strong>Modifier</strong>: There is a single modifier
  73.  *        which is used to specify that all accents (secondary differences) are
  74.  *        backwards.
  75.  *        <p>'@' : Indicates that accents are sorted backwards, as in French.
  76.  *    <LI><strong>Relation</strong>: The relations are the following:
  77.  *        <UL Type=square>
  78.  *            <LI>'<' : Greater, as a letter difference (primary)
  79.  *            <LI>';' : Greater, as an accent difference (secondary)
  80.  *            <LI>',' : Greater, as a case difference (tertiary)
  81.  *            <LI>'=' : Equal
  82.  *        </UL>
  83.  *    <LI><strong>Reset</strong>: There is a single reset
  84.  *        which is used primarily for contractions and expansions, but which
  85.  *        can also be used to add a modification at the end of a set of rules.
  86.  *        <p>'&' : Indicates that the next rule follows the position to where
  87.  *            the reset text-argument would be sorted.
  88.  * </UL>
  89.  *
  90.  * <p>
  91.  * This sounds more complicated than it is in practice. For example, the
  92.  * following are equivalent ways of expressing the same thing:
  93.  * <blockquote>
  94.  * <pre>
  95.  * a < b < c
  96.  * a < b & b < c
  97.  * a < c & a < b
  98.  * </pre>
  99.  * </blockquote>
  100.  * Notice that the order is important, as the subsequent item goes immediately
  101.  * after the text-argument. The following are not equivalent:
  102.  * <blockquote>
  103.  * <pre>
  104.  * a < b & a < c
  105.  * a < c & a < b
  106.  * </pre>
  107.  * </blockquote>
  108.  * Either the text-argument must already be present in the sequence, or some
  109.  * initial substring of the text-argument must be present. (e.g. "a < b & ae <
  110.  * e" is valid since "a" is present in the sequence before "ae" is reset). In
  111.  * this latter case, "ae" is not entered and treated as a single character;
  112.  * instead, "e" is sorted as if it were expanded to two characters: "a"
  113.  * followed by an "e". This difference appears in natural languages: in
  114.  * traditional Spanish "ch" is treated as though it contracts to a single
  115.  * character (expressed as "c < ch < d"), while in traditional German "Σ"
  116.  * (a-umlaut) is treated as though it expands to two characters (expressed as
  117.  * "a & ae ; Σ < b").
  118.  *
  119.  * <p>
  120.  * <strong>Ignorable Characters</strong>
  121.  * <p>
  122.  * For ignorable characters, the first rule must start with a relation (the
  123.  * examples we have used above are really fragments; "a < b" really should be
  124.  * "< a < b"). If, however, the first relation is not "<", then all the all
  125.  * text-arguments up to the first "<" are ignorable. For example, ", - < a < b"
  126.  * makes "-" an ignorable character, as we saw earlier in the word
  127.  * "black-birds". In the samples for different languages, you see that most
  128.  * accents are ignorable.
  129.  *
  130.  * <p><strong>Normalization and Accents</strong>
  131.  * <p>
  132.  * The <code>Collator</code> object automatically normalizes text internally
  133.  * to separate accents from base characters where possible. This is done both when
  134.  * processing the rules, and when comparing two strings. <code>Collator</code>
  135.  * also uses the Unicode canonical mapping to ensure that combining sequences
  136.  * are sorted properly (for more information, see
  137.  * <A HREF="http://www.aw.com/devpress">The Unicode Standard, Version 2.0</A>.)</P>
  138.  *
  139.  * <p><strong>Errors</strong>
  140.  * <p>
  141.  * The following are errors:
  142.  * <UL Type=round>
  143.  *     <LI>A text-argument contains unquoted punctuation symbols
  144.  *        (e.g. "a < b-c < d").
  145.  *     <LI>A relation or reset character not followed by a text-argument
  146.  *        (e.g. "a < , b").
  147.  *     <LI>A reset where the text-argument (or an initial substring of the
  148.  *         text-argument) is not already in the sequence.
  149.  *         (e.g. "a < b & e < f")
  150.  * </UL>
  151.  * If you produce one of these errors, a <code>RuleBasedCollator</code> throws
  152.  * a <code>ParseException</code>.
  153.  *
  154.  * <p><strong>Examples</strong>
  155.  * <p>Simple:     "< a < b < c < d"
  156.  * <p>Norwegian:  "< a,A< b,B< c,C< d,D< e,E< f,F< g,G< h,H< i,I< j,J
  157.  *                 < k,K< l,L< m,M< n,N< o,O< p,P< q,Q< r,R< s,S< t,T
  158.  *                 < u,U< v,V< w,W< x,X< y,Y< z,Z
  159.  *                 < \u00E5=a\u030A,\u00C5=A\u030A
  160.  *                 ;aa,AA< \u00E6,\u00C6< \u00F8,\u00D8"
  161.  *
  162.  * <p>
  163.  * Normally, to create a rule-based Collator object, you will use
  164.  * <code>Collator</code>'s factory method <code>getInstance</code>.
  165.  * However, to create a rule-based Collator object with specialized
  166.  * rules tailored to your needs, you construct the <code>RuleBasedCollator</code>
  167.  * with the rules contained in a <code>String</code> object. For example:
  168.  * <blockquote>
  169.  * <pre>
  170.  * String Simple = "< a < b < c < d";
  171.  * RuleBasedCollator mySimple = new RuleBasedCollator(Simple);
  172.  * </pre>
  173.  * </blockquote>
  174.  * Or:
  175.  * <blockquote>
  176.  * <pre>
  177.  * String Norwegian = "< a,A< b,B< c,C< d,D< e,E< f,F< g,G< h,H< i,I< j,J" +
  178.  *                 "< k,K< l,L< m,M< n,N< o,O< p,P< q,Q< r,R< s,S< t,T" +
  179.  *                 "< u,U< v,V< w,W< x,X< y,Y< z,Z" +
  180.  *                 "< \u00E5=a\u030A,\u00C5=A\u030A" +
  181.  *                 ";aa,AA< \u00E6,\u00C6< \u00F8,\u00D8";
  182.  * RuleBasedCollator myNorwegian = new RuleBasedCollator(Norwegian);
  183.  * </pre>
  184.  * </blockquote>
  185.  *
  186.  * <p>
  187.  * Combining <code>Collator</code>s is as simple as concatenating strings.
  188.  * Here's an example that combines two <code>Collator</code>s from two
  189.  * different locales:
  190.  * <blockquote>
  191.  * <pre>
  192.  * // Create an en_US Collator object
  193.  * RuleBasedCollator en_USCollator = (RuleBasedCollator)
  194.  *     Collator.getInstance(new Locale("en", "US", ""));
  195.  * // Create a da_DK Collator object
  196.  * RuleBasedCollator da_DKCollator = (RuleBasedCollator)
  197.  *     Collator.getInstance(new Locale("da", "DK", ""));
  198.  * // Combine the two
  199.  * // First, get the collation rules from en_USCollator
  200.  * String en_USRules = en_USCollator.getRules();
  201.  * // Second, get the collation rules from da_DKCollator
  202.  * String da_DKRules = da_DKCollator.getRules();
  203.  * RuleBasedCollator newCollator =
  204.  *     new RuleBasedCollator(en_USRules + da_DKRules);
  205.  * // newCollator has the combined rules
  206.  * </pre>
  207.  * </blockquote>
  208.  *
  209.  * <p>
  210.  * Another more interesting example would be to make changes on an existing
  211.  * table to create a new <code>Collator</code> object.  For example, add
  212.  * "& C < ch, cH, Ch, CH" to the <code>en_USCollator</code> object to create
  213.  * your own:
  214.  * <blockquote>
  215.  * <pre>
  216.  * // Create a new Collator object with additional rules
  217.  * String addRules = "& C < ch, cH, Ch, CH";
  218.  * RuleBasedCollator myCollator =
  219.  *     new RuleBasedCollator(en_USCollator + addRules);
  220.  * // myCollator contains the new rules
  221.  * </pre>
  222.  * </blockquote>
  223.  *
  224.  * <p>
  225.  * The following example demonstrates how to change the order of
  226.  * non-spacing accents,
  227.  * <blockquote>
  228.  * <pre>
  229.  * // old rule
  230.  * String oldRules = "=\u0301;\u0300;\u0302;\u0308"    // main accents
  231.  *                 + ";\u0327;\u0303;\u0304;\u0305"    // main accents
  232.  *                 + ";\u0306;\u0307;\u0309;\u030A"    // main accents
  233.  *                 + ";\u030B;\u030C;\u030D;\u030E"    // main accents
  234.  *                 + ";\u030F;\u0310;\u0311;\u0312"    // main accents
  235.  *                 + "< a , A ; ae, AE ; \u00e6 , \u00c6"
  236.  *                 + "< b , B < c, C < e, E & C < d, D";
  237.  * // change the order of accent characters
  238.  * String addOn = "& \u0300 ; \u0308 ; \u0302";
  239.  * RuleBasedCollator myCollator = new RuleBasedCollator(oldRules + addOn);
  240.  * </pre>
  241.  * </blockquote>
  242.  *
  243.  * <p>
  244.  * The last example shows how to put new primary ordering in before the
  245.  * default setting. For example, in Japanese <code>Collator</code>, you
  246.  * can either sort English characters before or after Japanese characters,
  247.  * <blockquote>
  248.  * <pre>
  249.  * // get en_US Collator rules
  250.  * RuleBasedCollator en_USCollator = (RuleBasedCollator)Collator.getInstance(Locale.US);
  251.  * // add a few Japanese character to sort before English characters
  252.  * // suppose the last character before the first base letter 'a' in
  253.  * // the English collation rule is \u2212
  254.  * String jaString = "& \u2212 < \u3041, \u3042 < \u3043, \u3044";
  255.  * RuleBasedCollator myJapaneseCollator = new
  256.  *     RuleBasedCollator(en_USCollator.getRules() + jaString);
  257.  * </pre>
  258.  * </blockquote>
  259.  *
  260.  * @see        Collator
  261.  * @see        CollationElementIterator
  262.  * @version    1.21 02/12/98
  263.  * @author     Helena Shih
  264.  */
  265. public class RuleBasedCollator extends Collator{
  266.     //===========================================================================================
  267.     //  The following diagram shows the data structure of the RuleBasedCollator object.
  268.     //  Suppose we have the rule, where 'o-umlaut' is the unicode char 0x00F6.
  269.     //  "a, A < b, B < c, C, ch, cH, Ch, CH < d, D ... < o, O; 'o-umlaut'/E, 'O-umlaut'/E ...".
  270.     //  What the rule says is, sorts 'ch'ligatures and 'c' only with tertiary difference and
  271.     //  sorts 'o-umlaut' as if it's always expanded with 'e'.
  272.     //
  273.     // mapping table                     contracting list           expanding list
  274.     // (contains all unicode char
  275.     //  entries)                   ___    ____________       _________________________
  276.     //  ________                +>|_*_|->|'c' |v('c') |  +>|v('o')|v('umlaut')|v('e')|
  277.     // |_\u0001_|-> v('\u0001') | |_:_|  |------------|  | |-------------------------|
  278.     // |_\u0002_|-> v('\u0002') | |_:_|  |'ch'|v('ch')|  | |             :           |
  279.     // |____:___|               | |_:_|  |------------|  | |-------------------------|
  280.     // |____:___|               |        |'cH'|v('cH')|  | |             :           |
  281.     // |__'a'___|-> v('a')      |        |------------|  | |-------------------------|
  282.     // |__'b'___|-> v('b')      |        |'Ch'|v('Ch')|  | |             :           |
  283.     // |____:___|               |        |------------|  | |-------------------------|
  284.     // |____:___|               |        |'CH'|v('CH')|  | |             :           |
  285.     // |___'c'__|----------------         ------------   | |-------------------------|
  286.     // |____:___|                                        | |             :           |
  287.     // |o-umlaut|----------------------------------------  |_________________________|
  288.     // |____:___|
  289.     //
  290.     // Noted by Helena Shih on 6/23/97
  291.     //============================================================================================
  292.  
  293.     /**
  294.      * RuleBasedCollator constructor.  This takes the table rules and builds
  295.      * a collation table out of them.  Please see RuleBasedCollator class
  296.      * description for more details on the collation rule syntax.
  297.      * @see java.util.Locale
  298.      * @param rules the collation rules to build the collation table from.
  299.      * @exception ParseException A format exception
  300.      * will be thrown if the build process of the rules fails. For
  301.      * example, build rule "a < ? < d" will cause the constructor to
  302.      * throw the ParseException because the '?' is not quoted.
  303.      */
  304.     public RuleBasedCollator(String rules) throws ParseException {
  305.         setStrength(Collator.TERTIARY);
  306.         build(rules);
  307.     }
  308.  
  309.     /**
  310.      * Gets the table-based rules for the collation object.
  311.      * @return returns the collation rules that the table collation object
  312.      * was created from.
  313.      */
  314.     public String getRules()
  315.     {
  316.         if (ruleTable == null) {
  317.             ruleTable = mPattern.emitPattern();
  318.             mPattern = null;
  319.         }
  320.         return ruleTable;
  321.     }
  322.  
  323.  
  324.     /**
  325.      * Return a CollationElementIterator for the given String.
  326.      * @see java.text.CollationElementIterator
  327.      */
  328.     public CollationElementIterator getCollationElementIterator(String source) {
  329.         return new CollationElementIterator( source, this );
  330.     }
  331.  
  332.     /**
  333.      * Compares the character data stored in two different strings based on the
  334.      * collation rules.  Returns information about whether a string is less
  335.      * than, greater than or equal to another string in a language.
  336.      * This can be overriden in a subclass.
  337.      */
  338.     public int compare(String source, String target)
  339.     {
  340.         // The basic algorithm here is that we use CollationElementIterators
  341.         // to step through both the source and target strings.  We compare each
  342.         // collation element in the source string against the corresponding one
  343.         // in the target, checking for differences.
  344.         //
  345.         // If a difference is found, we set <result> to LESS or GREATER to
  346.         // indicate whether the source string is less or greater than the target.
  347.         //
  348.         // However, it's not that simple.  If we find a tertiary difference
  349.         // (e.g. 'A' vs. 'a') near the beginning of a string, it can be
  350.         // overridden by a primary difference (e.g. "A" vs. "B") later in
  351.         // the string.  For example, "AA" < "aB", even though 'A' > 'a'.
  352.         //
  353.         // To keep track of this, we use strengthResult to keep track of the
  354.         // strength of the most significant difference that has been found
  355.         // so far.  When we find a difference whose strength is greater than
  356.         // strengthResult, it overrides the last difference (if any) that
  357.         // was found.
  358.  
  359.         int result = Collator.EQUAL;
  360.         strengthResult = Collator.IDENTICAL;
  361.         CollationElementIterator targetCursor
  362.             = new CollationElementIterator(target, this);
  363.         CollationElementIterator sourceCursor
  364.             = new CollationElementIterator(source, this);
  365.         int sOrder = 0, tOrder = 0;
  366.         int savedSOrder = 0, savedTOrder = 0;
  367.         boolean skipSecCheck = false;
  368.         boolean gets = true, gett = true;
  369.         while(true) {
  370.             // Get the next collation element in each of the strings, unless
  371.             // we've been requested to skip it.
  372.             int pSOrder = 0, pTOrder = 0;
  373.             if (gets) sOrder = sourceCursor.next(); else gets = true;
  374.             if (gett) tOrder = targetCursor.next(); else gett = true;
  375.  
  376.             // If we've hit the end of one of the strings, jump out of the loop
  377.             if ((sOrder == CollationElementIterator.NULLORDER)||
  378.                 (tOrder == CollationElementIterator.NULLORDER))
  379.                 break;
  380.  
  381.             // When we hit the end of one of the strings, we're going to need to remember
  382.             // the last element in each string, in order to decide if there
  383.             savedSOrder = sOrder;
  384.             savedTOrder = tOrder;
  385.  
  386.             // If there's no difference at this position, we can skip it
  387.             if (sOrder == tOrder)
  388.                 continue;
  389.  
  390.             // Compare primary differences first.
  391.             pSOrder = CollationElementIterator.primaryOrder(sOrder);
  392.             pTOrder = CollationElementIterator.primaryOrder(tOrder);
  393.             if ( pSOrder != pTOrder )
  394.             {
  395.                 if (sOrder == 0) {
  396.                     // The entire source element is ignorable.  Skip to the
  397.                     //next source element, but don't fetch another target element.
  398.                     gett = false;
  399.                     continue;
  400.                 }
  401.                 if (tOrder == 0) {
  402.                     gets = false;
  403.                     continue;
  404.                 }
  405.  
  406.                 // Neither the source or target order is totally ignorable,
  407.                 // but it's still possible for the primary component of one of the
  408.                 // elements to be ignorable, e.g. for a combining accent mark
  409.  
  410.                 if (pSOrder == 0)    // primary order in source is ignorable
  411.                 {
  412.                     if (pTOrder == 0)  // primary order in target is ignorable
  413.                     {
  414.                         // check the secondary/tertiary weight when both are ignorable chars.
  415.                         result = checkSecTerDiff(sOrder, tOrder, result, false);
  416.  
  417.                         // We already checked the secondary weights, so don't do it again
  418.                         skipSecCheck = true;
  419.                         continue;                   // both advances
  420.                     }
  421.                     else
  422.                     {
  423.                         //
  424.                         // The source's primary is ignorable, but the target's isn't.  We treat
  425.                         // ignorables as a secondary difference, so remember that we found one.
  426.                         // BUT, for French secondary ordering we might have already found a secondary
  427.                         // difference in the ignorables attached to this base char.  If we have, we
  428.                         // don't need to mark a difference here.
  429.                         //
  430.                         if ((!isFrenchSec &&
  431.                              (result == Collator.EQUAL || strengthResult > Collator.SECONDARY)) ||
  432.                             (isFrenchSec && !skipSecCheck))
  433.                         {
  434.                             strengthResult = Collator.SECONDARY;
  435.                             result = Collator.GREATER;
  436.                         }
  437.                         // Skip to the next source element, but don't fetch another target element.
  438.                         gett = false;
  439.                         skipSecCheck = false;
  440.                         continue;
  441.                     }
  442.                 }
  443.                 else if (pTOrder == 0)
  444.                 {
  445.                     // record differences - see the comment above.
  446.                     if ((!isFrenchSec &&
  447.                          (result == Collator.EQUAL || strengthResult > Collator.SECONDARY)) ||
  448.                         (isFrenchSec && !skipSecCheck))
  449.                      {
  450.                         result = Collator.LESS;
  451.                         strengthResult = Collator.SECONDARY;
  452.                     }
  453.                     gets = false;
  454.                     skipSecCheck = false;
  455.                     continue;
  456.                 }
  457.                 //
  458.                 // Neither of the orders is ignorable, and we already know that the primary
  459.                 // orders are different because of the (pSOrder != pTOrder) test above.
  460.                 // Record the difference and stop the comparison.
  461.                 //
  462.                 if (pSOrder < pTOrder)
  463.                     result = Collator.LESS;
  464.                 else
  465.                     result = Collator.GREATER;
  466.                 break;
  467.             }
  468.             else {
  469.                 //
  470.                 // The primary orders are the same, but we need to continue to check
  471.                 // for secondary or tertiary differences.
  472.                 //
  473.                 result = checkSecTerDiff(sOrder, tOrder, result, skipSecCheck);
  474.                 if (isFrenchSec &&
  475.                     CollationElementIterator.isIgnorable(sOrder) &&
  476.                     CollationElementIterator.isIgnorable(tOrder))
  477.                     skipSecCheck = true;
  478.                 else
  479.                     skipSecCheck = false;
  480.             }
  481.         } // while()
  482.  
  483.         if (sOrder != CollationElementIterator.NULLORDER) {
  484.             if (tOrder == CollationElementIterator.NULLORDER) {
  485.                 // The source string hasn't not reached the end and target string has...
  486.                 do {
  487.                     if (CollationElementIterator.primaryOrder(sOrder) != 0) {
  488.                         // We found a non-ignorable base character in the source string.
  489.                         // This is a primary difference, so the source is greater
  490.                         return 1;
  491.                     } else if (CollationElementIterator.secondaryOrder(sOrder) != 0) {
  492.                         //
  493.                         // If the last character in the target string was a base character,
  494.                         // or if we haven't found any secondary differences yet,
  495.                         // we still need to look at accent marks in the source string, because
  496.                         // they can affect the result in languages with reversed (French)
  497.                         // secondary ordering.
  498.                         //
  499.                         if (!CollationElementIterator.isIgnorable(savedTOrder) ||
  500.                             strengthResult > Collator.SECONDARY)
  501.                             result = checkSecTerDiff(sOrder, 0, result, false);
  502.                         else
  503.                             continue;
  504.                     }
  505.                 } while ((sOrder = sourceCursor.next()) != CollationElementIterator.NULLORDER);
  506.             }
  507.         }
  508.         else if (tOrder != CollationElementIterator.NULLORDER) {
  509.             // See comments above.
  510.             do {
  511.                 if (CollationElementIterator.primaryOrder(tOrder) != 0) {
  512.                     return -1;
  513.                 } else if (CollationElementIterator.secondaryOrder(tOrder) != 0) {
  514.                     if (!CollationElementIterator.isIgnorable(savedSOrder) ||
  515.                         strengthResult > Collator.SECONDARY)
  516.                         result = checkSecTerDiff(0, tOrder, result, false);
  517.                     else
  518.                         continue;
  519.                 }
  520.             } while ((tOrder = targetCursor.next()) != CollationElementIterator.NULLORDER);
  521.         }
  522.  
  523.         // For IDENTICAL comparisons, we use a bitwise character comparison
  524.         // as a tiebreaker if all else is equal
  525.         if (result == 0 && getStrength() == IDENTICAL) {
  526.             result = DecompositionIterator.decompose(source,getDecomposition())
  527.                 .compareTo(DecompositionIterator.decompose(target,getDecomposition()));
  528.         }
  529.         return result;
  530.     }
  531.     /**
  532.      * Transforms the string into a series of characters that can be compared
  533.      * with CollationKey.compareTo. This overrides java.text.Collator.getCollationKey.
  534.      * It can be overriden in a subclass.
  535.      */
  536.     public CollationKey getCollationKey(String source)
  537.     {
  538.         //
  539.         // The basic algorithm here is to find all of the collation elements for each
  540.         // character in the source string, convert them to a char representation,
  541.         // and put them into the collation key.  But it's trickier than that.
  542.         // Each collation element in a string has three components: primary (A vs B),
  543.         // secondary (A vs A-acute), and tertiary (A' vs a); and a primary difference
  544.         // at the end of a string takes precedence over a secondary or tertiary
  545.         // difference earlier in the string.
  546.         //
  547.         // To account for this, we put all of the primary orders at the beginning of the
  548.         // string, followed by the secondary and tertiary orders, separated by nulls.
  549.         //
  550.         // Here's a hypothetical example, with the collation element represented as
  551.         // a three-digit number, one digit for primary, one for secondary, etc.
  552.         //
  553.         // String:              A     a     B    Θ <--(e-acute)
  554.         // Collation Elements: 101   100   201  510
  555.         //
  556.         // Collation Key:      1125<null>0001<null>1010
  557.         //
  558.         // To make things even trickier, secondary differences (accent marks) are compared
  559.         // starting at the *end* of the string in languages with French secondary ordering.
  560.         // But when comparing the accent marks on a single base character, they are compared
  561.         // from the beginning.  To handle this, we reverse all of the accents that belong
  562.         // to each base character, then we reverse the entire string of secondary orderings
  563.         // at the end.  Taking the same example above, a French collator might return
  564.         // this instead:
  565.         //
  566.         // Collation Key:      1125<null>1000<null>1010
  567.         //
  568.         if (source == null)
  569.             return null;
  570.         primResult.setLength(0);
  571.         secResult.setLength(0);
  572.         terResult.setLength(0);
  573.         int order = 0;
  574.         boolean compareSec = (getStrength() >= Collator.SECONDARY);
  575.         boolean compareTer = (getStrength() >= Collator.TERTIARY);
  576.         int secOrder = CollationElementIterator.NULLORDER;
  577.         int terOrder = CollationElementIterator.NULLORDER;
  578.         int preSecIgnore = 0;
  579.  
  580.         CollationElementIterator sourceCursor = new
  581.             CollationElementIterator(source, this);
  582.  
  583.         // walk through each character
  584.         while ((order = sourceCursor.next()) !=
  585.                CollationElementIterator.NULLORDER)
  586.         {
  587.             secOrder = CollationElementIterator.secondaryOrder(order);
  588.             terOrder = CollationElementIterator.tertiaryOrder(order);
  589.             if (!CollationElementIterator.isIgnorable(order))
  590.             {
  591.                 primResult.append((char) (CollationElementIterator.primaryOrder(order)
  592.                                     + COLLATIONKEYOFFSET));
  593.  
  594.                 if (compareSec) {
  595.                     //
  596.                     // accumulate all of the ignorable/secondary characters attached
  597.                     // to a given base character
  598.                     //
  599.                     if (isFrenchSec && preSecIgnore < secResult.length()) {
  600.                         //
  601.                         // We're doing reversed secondary ordering and we've hit a base
  602.                         // (non-ignorable) character.  Reverse any secondary orderings
  603.                         // that applied to the last base character.  (see block comment above.)
  604.                         //
  605.                         reverse(secResult, preSecIgnore, secResult.length());
  606.                     }
  607.                     // Remember where we are in the secondary orderings - this is how far
  608.                     // back to go if we need to reverse them later.
  609.                     secResult.append((char)(secOrder+ COLLATIONKEYOFFSET));
  610.                     preSecIgnore = secResult.length();
  611.                 }
  612.                 if (compareTer) {
  613.                     terResult.append((char)(terOrder+ COLLATIONKEYOFFSET));
  614.                 }
  615.             }
  616.             else
  617.             {
  618.                 if (compareSec && secOrder != 0)
  619.                     secResult.append((char)
  620.                         (secOrder+maxSecOrder+ COLLATIONKEYOFFSET));
  621.                 if (compareTer && terOrder != 0)
  622.                     terResult.append((char)
  623.                         (terOrder+maxTerOrder+ COLLATIONKEYOFFSET));
  624.             }
  625.         }
  626.         if (isFrenchSec)
  627.         {
  628.             if (preSecIgnore < secResult.length()) {
  629.                 // If we've accumlated any secondary characters after the last base character,
  630.                 // reverse them.
  631.                 reverse(secResult, preSecIgnore, secResult.length());
  632.             }
  633.             // And now reverse the entire secResult to get French secondary ordering.
  634.             reverse(secResult, 0, secResult.length());
  635.         }
  636.         primResult.append((char)0);
  637.         secResult.append((char)0);
  638.         secResult.append(terResult.toString());
  639.         primResult.append(secResult.toString());
  640.  
  641.         if (getStrength() == IDENTICAL) {
  642.             primResult.append((char)0);
  643.             primResult.append(DecompositionIterator.decompose(source,getDecomposition()));
  644.         }
  645.         return new CollationKey(source, primResult.toString());
  646.     }
  647.     /**
  648.      * Standard override; no change in semantics.
  649.      */
  650.     public Object clone() {
  651.         RuleBasedCollator other = (RuleBasedCollator) super.clone();
  652.         other.primResult = new StringBuffer(MAXTOKENLEN);
  653.         other.secResult = new StringBuffer(MAXTOKENLEN);
  654.         other.terResult = new StringBuffer(MAXTOKENLEN);
  655.         other.key = new StringBuffer(MAXKEYSIZE);
  656.         return other;
  657.     }
  658.  
  659.     /**
  660.      * Compares the equality of two collation objects.
  661.      * @param obj the table-based collation object to be compared with this.
  662.      * @return true if the current table-based collation object is the same
  663.      * as the table-based collation object obj; false otherwise.
  664.      */
  665.     public boolean equals(Object obj) {
  666.         if (obj == null) return false;
  667.         if (!super.equals(obj)) return false;  // super does class check
  668.         RuleBasedCollator other = (RuleBasedCollator) obj;
  669.         // all other non-transient information is also contained in rules.
  670.         return (getRules().equals(other.getRules()));
  671.     }
  672.     /**
  673.      * Generates the hash code for the table-based collation object
  674.      */
  675.     public int hashCode() {
  676.         return getRules().hashCode();
  677.     }
  678.  
  679.     // ==============================================================
  680.     // private
  681.     // ==============================================================
  682.  
  683.     /**
  684.      * Create a table-based collation object with the given rules.
  685.      * @see java.util.RuleBasedCollator#RuleBasedCollator
  686.      * @exception ParseException If the rules format is incorrect.
  687.      */
  688.     private void build(String pattern) throws ParseException
  689.     {
  690.         int aStrength = Collator.IDENTICAL;
  691.         boolean isSource = true;
  692.         int i = 0;
  693.         String expChars;
  694.         String groupChars;
  695.         if (pattern.length() == 0)
  696.             throw new ParseException("Build rules empty.", 0);
  697.  
  698.         // This array maps Unicode characters to their collation ordering
  699.         mapping = new CompactIntArray((int)UNMAPPED);
  700.  
  701.         // Normalize the build rules.  Find occurances of all decomposed characters
  702.         // and normalize the rules before feeding into the builder.  By "normalize",
  703.         // we mean that all precomposed Unicode characters must be converted into
  704.         // a base character and one or more combining characters (such as accents).
  705.         // When there are multiple combining characters attached to a base character,
  706.         // the combining characters must be in their canonical order
  707.         //
  708.         pattern = DecompositionIterator.decompose(pattern, getDecomposition());
  709.  
  710.         // Build the merged collation entries
  711.         // Since rules can be specified in any order in the string
  712.         // (e.g. "c , C < d , D < e , E .... C < CH")
  713.         // this splits all of the rules in the string out into separate
  714.         // objects and then sorts them.  In the above example, it merges the
  715.         // "C < CH" rule in just before the "C < D" rule.
  716.         //
  717.         mPattern = new MergeCollation(pattern);
  718.  
  719.         // Now walk though each entry and add it to my own tables
  720.         for (i = 0; i < mPattern.getCount(); ++i)
  721.         {
  722.             PatternEntry entry = mPattern.getItemAt(i);
  723.             if (entry != null) {
  724.                 groupChars = entry.getChars();
  725.                 if ((groupChars.length() > 1) &&
  726.                     (groupChars.charAt(groupChars.length()-1) == '@')) {
  727.                     isFrenchSec = true;
  728.                     groupChars = groupChars.substring(0, groupChars.length()-1);
  729.                 }
  730.                 expChars = entry.getExtension();
  731.                 if (expChars.length() != 0) {
  732.                     addExpandOrder(groupChars, expChars, entry.getStrength());
  733.                 } else if (groupChars.length() > 1) {
  734.                     addContractOrder(groupChars, entry.getStrength());
  735.                     lastChar = groupChars.charAt(0);
  736.                 } else {
  737.                     char ch = groupChars.charAt(0);
  738.                     addOrder(ch, entry.getStrength());
  739.                     lastChar = ch;
  740.                 }
  741.             }
  742.         }
  743.         commit();
  744.         mapping.compact();
  745.     }
  746.     /**
  747.      * Look up for unmapped values in the expanded character table.
  748.      */
  749.     private final void commit()
  750.     {
  751.     // When the expanding character tables are built by addExpandOrder,
  752.     // it doesn't know what the final ordering of each character
  753.     // in the expansion will be.  Instead, it just puts the raw character
  754.     // code into the table, adding CHARINDEX as a flag.  Now that we've
  755.     // finished building the mapping table, we can go back and look up
  756.     // that character to see what its real collation order is and
  757.     // stick that into the expansion table.  That lets us avoid doing
  758.     // a two-stage lookup later.
  759.  
  760.         if (expandTable != null)
  761.         {
  762.             for (int i = 0; i < expandTable.size(); i++)
  763.             {
  764.                 int[] valueList = (int [])expandTable.elementAt(i);
  765.                 for (int j = 0; j < valueList.length; j++)
  766.                 {
  767.                     if ((valueList[j] < EXPANDCHARINDEX) &&
  768.                              (valueList[j] > CHARINDEX))
  769.                     {
  770.                         // found a expanding character
  771.                         // the expanding char value is not filled in yet
  772.                         char ch = (char)(valueList[j] - CHARINDEX);
  773.  
  774.                         // Get the real values for the non-filled entry
  775.                         int realValue = mapping.elementAt(ch);
  776.  
  777.                         if (realValue == UNMAPPED)
  778.                         {
  779.                             // The real value is still unmapped, maybe it's an ignorable
  780.                             // char
  781.                             valueList[j] = IGNORABLEMASK & valueList[j-1];
  782.                         }
  783.                         else if (realValue >= CONTRACTCHARINDEX)
  784.                         {
  785.                                 // if the entry is actually pointing to a contracting char
  786.                             EntryPair pair = null;
  787.                             Vector groupList = (Vector)
  788.                                 contractTable.elementAt(realValue
  789.                                                         - CONTRACTCHARINDEX);
  790.                             pair = (EntryPair)groupList.firstElement();
  791.                             valueList[j] = pair.value;
  792.                         }
  793.                         else
  794.                         {
  795.                             // just fill in the value
  796.                             valueList[j] = realValue;
  797.                         }
  798.                     }
  799.                 }
  800.             }
  801.         }
  802.     }
  803.     /**
  804.      *  Increment of the last order based on the comparison level.
  805.      */
  806.     private final int increment(int aStrength, int lastValue)
  807.     {
  808.         switch(aStrength)
  809.         {
  810.         case Collator.PRIMARY:
  811.             // increment priamry order  and mask off secondary and tertiary difference
  812.             lastValue += PRIMARYORDERINCREMENT;
  813.             lastValue &= PRIMARYORDERMASK;
  814.             isOverIgnore = true;
  815.             break;
  816.         case Collator.SECONDARY:
  817.             // increment secondary order and mask off tertiary difference
  818.             lastValue += SECONDARYORDERINCREMENT;
  819.             lastValue &= SECONDARYDIFFERENCEONLY;
  820.             // record max # of ignorable chars with secondary difference
  821.             if (!isOverIgnore)
  822.                 maxSecOrder++;
  823.             break;
  824.         case Collator.TERTIARY:
  825.             // increment tertiary order
  826.             lastValue += TERTIARYORDERINCREMENT;
  827.             // record max # of ignorable chars with tertiary difference
  828.             if (!isOverIgnore)
  829.                 maxTerOrder++;
  830.             break;
  831.         }
  832.         return lastValue;
  833.     }
  834.  
  835.     /**
  836.      *  Adds a character and its designated order into the collation table.
  837.      */
  838.     private final void addOrder(char ch,
  839.                                 int aStrength)
  840.     {
  841.         // See if the char already has an order in the mapping table
  842.         int order = mapping.elementAt(ch);
  843.  
  844.         if (order >= CONTRACTCHARINDEX) {
  845.             // There's already an entry for this character that points to a contracting
  846.             // character table.  Instead of adding the character directly to the mapping
  847.             // table, we must add it to the contract table instead.
  848.  
  849.             key.setLength(0);
  850.             key.append(ch);
  851.             addContractOrder(key.toString(), aStrength);
  852.         } else {
  853.             // add the entry to the mapping table,
  854.             // the same later entry replaces the previous one
  855.             currentOrder = increment(aStrength, currentOrder);
  856.             mapping.setElementAt(ch, currentOrder);
  857.         }
  858.     }
  859.     /**
  860.      *  Adds the contracting string into the collation table.
  861.      */
  862.     private final void addContractOrder(String groupChars,
  863.                                   int   aStrength)
  864.     {
  865.         if (contractTable == null) {
  866.             contractTable = new Vector(INITIALTABLESIZE);
  867.         }
  868.  
  869.         // Figure out what ordering to give this new entry
  870.         if (aStrength != IDENTICAL) {
  871.             currentOrder = increment(aStrength, currentOrder);
  872.         }
  873.  
  874.         // See if the initial character of the string already has a contract table.
  875.         int entry = mapping.elementAt(groupChars.charAt(0));
  876.         Vector entryTable = getContractValues(entry - CONTRACTCHARINDEX);
  877.  
  878.         if (entryTable != null) {
  879.             int index = getEntry(entryTable, groupChars);
  880.  
  881.             if (index != UNMAPPED) {
  882.                 // If there was already a contracting table for this character,
  883.                 // we simply want to add (or replace) this string in it
  884.                 EntryPair pair = (EntryPair) entryTable.elementAt(index);
  885.                 pair.value = currentOrder;
  886.             } else {
  887.                 entryTable.addElement(new EntryPair(groupChars, currentOrder));
  888.             }
  889.         }
  890.         else
  891.         {
  892.             // We need to create a new table of contract entries
  893.             entryTable = new Vector(INITIALTABLESIZE);
  894.             int tableIndex = CONTRACTCHARINDEX + contractTable.size();
  895.  
  896.             // Always add the initial character's current ordering first.
  897.             entryTable.addElement(new EntryPair(groupChars.substring(0,1), entry));
  898.  
  899.             // And add the new one
  900.             entryTable.addElement(new EntryPair(groupChars, currentOrder));
  901.  
  902.             // Finally, add the new value table to the main contract table
  903.             // and update this character's mapping to point to it.
  904.             contractTable.addElement(entryTable);
  905.             mapping.setElementAt(groupChars.charAt(0), tableIndex);
  906.         }
  907.     }
  908.  
  909.     private final int getEntry(Vector list, String name) {
  910.         for (int i = 0; i < list.size(); i++) {
  911.             EntryPair pair = (EntryPair)list.elementAt(i);
  912.             if (pair.entryName.equals(name)) {
  913.                 return i;
  914.             }
  915.         }
  916.         return UNMAPPED;
  917.     }
  918.     /**
  919.      *  Get the entry of hash table of the contracting string in the collation
  920.      *  table.
  921.      *  @param ch the starting character of the contracting string
  922.      */
  923.     Vector getContractValues(char ch)
  924.     {
  925.         int index = mapping.elementAt(ch);
  926.         return getContractValues(index - CONTRACTCHARINDEX);
  927.     }
  928.  
  929.     Vector getContractValues(int index)
  930.     {
  931.         if (index >= 0)
  932.         {
  933.             return (Vector)contractTable.elementAt(index);
  934.         }
  935.         else // not found
  936.         {
  937.             return null;
  938.         }
  939.     }
  940.  
  941.     /**
  942.      *  Adds the expanding string into the collation table.
  943.      */
  944.     private final void addExpandOrder(String contractChars,
  945.                                 String expandChars,
  946.                                 int   aStrength) throws ParseException
  947.     {
  948.         EntryPair pair = new EntryPair();
  949.  
  950.         // Make a expanding char table if there's not one.
  951.         if (expandTable == null)
  952.         {
  953.             expandTable = new Vector(INITIALTABLESIZE);
  954.         }
  955.  
  956.         // For expanding characters, what we stick into the main mapping table
  957.         // is the character's index in the expand table, plus a flag to indicate
  958.         // that it's an  expanding character.
  959.         int tmpValue = EXPANDCHARINDEX + expandTable.size();
  960.  
  961.         // need to check if the entry is key or not later
  962.         key.setLength(0);
  963.         int keyValue = UNMAPPED;
  964.  
  965.         if (contractChars.length() > 1)
  966.         {
  967.             // This entry is actually a string of characters that contract
  968.             // and then expand back into a different string.
  969.             // First, we have to make sure that the entry is in the contract table
  970.             //
  971.             addContractOrder(contractChars, aStrength);
  972.  
  973.             // Remember the character we just added, so that the code in build() can use it
  974.             // to decide where to put the next character.
  975.             lastChar = contractChars.charAt(0);
  976.  
  977.             // Now that there's a contracting-table entry for this key, set its value
  978.             // to the expanding character sequence
  979.             Vector list = getContractValues(contractChars.charAt(0));
  980.             int entry = UNMAPPED;
  981.             entry = getEntry(list, contractChars);
  982.             if (entry != UNMAPPED) {
  983.                 pair = (EntryPair)list.elementAt(entry);
  984.                 // Remember what this entry's old value was, for use below.
  985.                 keyValue = pair.value;
  986.             }
  987.             pair.entryName = contractChars;
  988.             pair.value = tmpValue;
  989.         }
  990.         else
  991.         {
  992.             // There's no contraction involved, just a single character expanding
  993.             // into several other characters.
  994.  
  995.             char ch = contractChars.charAt(0);
  996.             if ((keyValue = mapping.elementAt(ch)) == UNMAPPED) {
  997.                 // This character doesn't have an entry in the mapping table yet,
  998.                 // so make one for it.
  999.                 addOrder(ch, aStrength);
  1000.                 lastChar = ch;
  1001.                 // Remember the ordering that we just created for this character...
  1002.                 keyValue = mapping.elementAt(lastChar);
  1003.             } else {
  1004.                 // This character already had an ordering, which we don't want to disturb,
  1005.                 // so create a new one to use.
  1006.                 keyValue = increment(aStrength, mapping.elementAt(lastChar));
  1007.             }
  1008.             mapping.setElementAt(ch, tmpValue);
  1009.         }
  1010.  
  1011.         // Create a list of the collation orders that this expands into...
  1012.         int[] valueList = new int[expandChars.length()+1];
  1013.         valueList[0] = keyValue;
  1014.  
  1015.         for (int i = 0; i < expandChars.length(); i++)
  1016.         {
  1017.             int mapValue = mapping.elementAt(expandChars.charAt(i));
  1018.             if (mapValue >= CONTRACTCHARINDEX)
  1019.             {
  1020.                 // if the expanding char is also a contracting char, look up the value
  1021.                 key.append(expandChars.charAt(i));
  1022.                 int foundValue = CHARINDEX + expandChars.charAt(i);
  1023.                 Vector list = getContractValues(expandChars.charAt(i));
  1024.                 if (list != null) {
  1025.                     int entry = UNMAPPED;
  1026.                     entry = getEntry(list, key.toString());
  1027.                     if (entry != UNMAPPED) {
  1028.                         pair = (EntryPair)list.elementAt(entry);
  1029.                         foundValue = pair.value;
  1030.                     }
  1031.                 }
  1032.                 key.setLength(0);
  1033.                 valueList[i+1] = foundValue;
  1034.             }
  1035.             else if (mapValue != UNMAPPED)
  1036.             {
  1037.                 // can't find it in the table, will be filled in by commit().
  1038.                 valueList[i+1] = mapValue;
  1039.             }
  1040.             else
  1041.             {
  1042.                 valueList[i+1] = CHARINDEX + (int)(expandChars.charAt(i));
  1043.             }
  1044.         }
  1045.         // Add the expanding char list into the table, finally.
  1046.         expandTable.addElement(valueList);
  1047.     }
  1048.  
  1049.     /**
  1050.      *  Get the entry of hash table of the expanding string in the collation
  1051.      *  table.
  1052.      *  @param ch the starting character of the expanding string
  1053.      */
  1054.     final int[] getExpandValueList(char ch)
  1055.     {
  1056.         int expIndex = mapping.elementAt(ch);
  1057.         if ((expIndex >= EXPANDCHARINDEX) &&
  1058.             (expIndex < CONTRACTCHARINDEX))
  1059.         {
  1060.             int tmpIndex = expIndex - EXPANDCHARINDEX;
  1061.             return (int[])expandTable.elementAt(tmpIndex);
  1062.         }
  1063.         else
  1064.         {
  1065.             return null;
  1066.         }
  1067.     }
  1068.     /**
  1069.      *  Get the entry of hash table of the expanding string in the collation
  1070.      *  table.
  1071.      *  @param idx the index of the expanding string value list
  1072.      */
  1073.     final int[] getExpandValueList(int idx)
  1074.     {
  1075.         if (idx < expandTable.size())
  1076.         {
  1077.             return (int[])expandTable.elementAt(idx);
  1078.         }
  1079.         else
  1080.         {
  1081.             return null;
  1082.         }
  1083.     }
  1084.     /**
  1085.      *  Get the comarison order of a character from the collation table.
  1086.      *  @return the comparison order of a character.
  1087.      */
  1088.     final int getUnicodeOrder(char ch)
  1089.     {
  1090.         return mapping.elementAt(ch);
  1091.     }
  1092.  
  1093.     /**
  1094.      * Check for the secondary and tertiary differences of source and
  1095.      * target comparison orders.
  1096.      * @return Collator.LESS if sOrder < tOrder; EQUAL if sOrder == tOrder;
  1097.      * Collator.GREATER if sOrder > tOrder.
  1098.      */
  1099.     private final int checkSecTerDiff(int sOrder,
  1100.                                       int tOrder,
  1101.                                       int result,
  1102.                                       boolean skipSecCheck)
  1103.     {
  1104.         int endResult = result;
  1105.         if (CollationElementIterator.secondaryOrder(sOrder) !=
  1106.             CollationElementIterator.secondaryOrder(tOrder))
  1107.         {
  1108.             if ((!isFrenchSec &&
  1109.                  (result == Collator.EQUAL || strengthResult > Collator.SECONDARY)) ||
  1110.                 (isFrenchSec && !skipSecCheck))
  1111.              {
  1112.                 strengthResult = Collator.SECONDARY;
  1113.                 if (CollationElementIterator.secondaryOrder(sOrder) <
  1114.                     CollationElementIterator.secondaryOrder(tOrder))
  1115.                     endResult = Collator.LESS;
  1116.                 else
  1117.                     endResult = Collator.GREATER;
  1118.             }
  1119.         }
  1120.         else if ((CollationElementIterator.tertiaryOrder(sOrder) !=
  1121.                   CollationElementIterator.tertiaryOrder(tOrder)) &&
  1122.                  (endResult == Collator.EQUAL))
  1123.         {
  1124.             strengthResult = Collator.TERTIARY;
  1125.             if (CollationElementIterator.tertiaryOrder(sOrder) <
  1126.                 CollationElementIterator.tertiaryOrder(tOrder))
  1127.                 endResult = Collator.LESS;
  1128.             else
  1129.                 endResult = Collator.GREATER;
  1130.         }
  1131.         return endResult;
  1132.     }
  1133.     /**
  1134.      * Reverse a string.
  1135.      */
  1136.     private final void reverse (StringBuffer result, int from, int to)
  1137.     {
  1138.         int i = from;
  1139.         char swap;
  1140.  
  1141.         int j = to - 1;
  1142.         while (i < j) {
  1143.             swap =  result.charAt(i);
  1144.             result.setCharAt(i, result.charAt(j));
  1145.             result.setCharAt(j, swap);
  1146.             i++;
  1147.             j--;
  1148.         }
  1149.     }
  1150.  
  1151.     // Proclaim compatibility with 1.1
  1152.     static final long serialVersionUID = 2822366911447564107L;
  1153.  
  1154.     static int CHARINDEX = 0x70000000;  // need look up in .commit()
  1155.     static int EXPANDCHARINDEX = 0x7E000000; // Expand index follows
  1156.     static int CONTRACTCHARINDEX = 0x7F000000;  // contract indexes follow
  1157.     static int UNMAPPED = 0xFFFFFFFF;
  1158.  
  1159.     private final static int SHORT_MAX_VALUE = 32767;
  1160.     private final static int PRIMARYORDERINCREMENT = 0x00010000;
  1161.     private final static int MAXIGNORABLE = 0x00010000;
  1162.     private final static int SECONDARYORDERINCREMENT = 0x00000100;
  1163.     private final static int TERTIARYORDERINCREMENT = 0x00000001;
  1164.     final static int PRIMARYORDERMASK = 0xffff0000;
  1165.     final static int SECONDARYORDERMASK = 0x0000ff00;
  1166.     final static int TERTIARYORDERMASK = 0x000000ff;
  1167.     final static int PRIMARYDIFFERENCEONLY = 0xffff0000;
  1168.     final static int SECONDARYDIFFERENCEONLY = 0xffffff00;
  1169.     private final static int SECONDARYRESETMASK = 0x0000ffff;
  1170.     private final static int IGNORABLEMASK = 0x0000ffff;
  1171.     private final static int INITIALTABLESIZE = 20;
  1172.     private final static int MAXKEYSIZE = 5;
  1173.     static int PRIMARYORDERSHIFT = 16;
  1174.     static int SECONDARYORDERSHIFT = 8;
  1175.     private final static int MAXTOKENLEN = 256;
  1176.     private final static int MAXRULELEN = 512;
  1177.     private final static int COLLATIONKEYOFFSET = 1;
  1178.  
  1179.     // these data members are reconstructed by readObject()
  1180.     private boolean isFrenchSec = false;
  1181.     private String ruleTable = null;
  1182.  
  1183.     private CompactIntArray mapping = null;
  1184.     private Vector   contractTable = null;
  1185.     private Vector   expandTable = null;
  1186.  
  1187.     // transients, only used in build or processing
  1188.     private transient MergeCollation mPattern = null;
  1189.     private transient boolean isOverIgnore = false;
  1190.     private transient int currentOrder = 0;
  1191.     private transient short maxSecOrder = 0;
  1192.     private transient short maxTerOrder = 0;
  1193.     private transient char lastChar;
  1194.     private transient StringBuffer key = new StringBuffer(MAXKEYSIZE);
  1195.     private transient int strengthResult = Collator.IDENTICAL;
  1196.     private transient StringBuffer primResult = new StringBuffer(MAXTOKENLEN);
  1197.     private transient StringBuffer secResult = new StringBuffer(MAXTOKENLEN);
  1198.     private transient StringBuffer terResult = new StringBuffer(MAXTOKENLEN);
  1199. }
  1200.  
  1201.